package org.judo.generate.mvc;

public class DatatableParser implements Parser
{
	ViewResult viewResult;
	DatatableInfo dataTable;
	private String curStatement;
	Parser nextParser;
	
	public ViewResult getViewResult()
	{
		return viewResult;
	}

	public void setViewResult(ViewResult viewResult)
	{
		this.viewResult = viewResult;
	}

	public Parser getNestedParser()
	{
		return nextParser;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/data_table"))
				return true;
				
		return false;
	}

	public boolean needsNestedParser()
	{		
		if (nextParser != null)
			return true;
		
		return false;
	}

	public void parseNextLine(String line) throws ParseException
	{
		curStatement = line;
		if (line.startsWith("/data_table"))
		{
			viewResult.html += "\t\t</table>\n";
		}
		else if (line.equals(""))
		{
		}
		else if (line.startsWith("var:"))
		{
			dataTable.var = ParserUtil.getValue(line,viewResult); 
		}
		else if (line.startsWith("items:"))
		{
			dataTable.items = ParserUtil.getValue(line,viewResult);
		}
		else if (line.startsWith("width:"))
		{
		}
		else if (line.startsWith("headers:"))
		{
			HeaderParser parser = new HeaderParser();
			nextParser = parser;
			parser.setViewResult(viewResult);
		}
		else if (line.startsWith("values:"))
		{
			ValuesParser parser = new ValuesParser();
			nextParser = parser;
			parser.setViewResult(viewResult);
			parser.setDataTable(dataTable);
		}
		else
		{
			throw new ParseException("Only 'headers:','values:','query:','query_all:' and 'width:' statements are allowed within the 'data_table:' statement");
		}
	}

	protected void doTextField(String line,String type)
		throws ParseException
	{
	}
	
	
	public void setInitLine(String line) throws ParseException
	{
		dataTable = new DatatableInfo();
		viewResult.dataTables.add(dataTable);
		
		viewResult.html += "\t\t<table class=\"standardTBL\">\n";
	}
}
